package com.iteaj.iboot.msn.iot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iteaj.framework.result.BooleanResult;
import com.iteaj.framework.result.DetailResult;
import com.iteaj.framework.result.PageResult;
import com.iteaj.framework.result.Result;
import com.iteaj.iboot.msn.iot.consts.DeviceStatus;
import com.iteaj.iboot.msn.iot.dto.CurrentDeviceDto;
import com.iteaj.iboot.msn.iot.dto.DeviceDto;
import com.iteaj.iboot.msn.iot.entity.Device;
import com.iteaj.iboot.msn.iot.mapper.DeviceMapper;
import com.iteaj.iboot.msn.iot.service.IDeviceService;
import com.iteaj.framework.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author iteaj
 * @since 2022-05-15
 */
@Service
public class DeviceServiceImpl extends BaseServiceImpl<DeviceMapper, Device> implements IDeviceService {

    @Override
    public Result<IPage<Device>> pageOfDetail(Page<Device> page, DeviceDto entity) {
        return new PageResult<>(getBaseMapper().pageOfDetail(page, entity));
    }

    @Override
    public BooleanResult save(Device entity) {
        Device device = this.getByDeviceSn(entity.getDeviceSn()).getData();
        if(device != null) {
            return new BooleanResult(false).fail("设备编号已经存在["+entity.getDeviceSn()+"]");
        }

        entity.setUid(IdWorker.getId());
        return super.save(entity);
    }

    @Override
    public BooleanResult updateById(Device entity) {
        Device device = this.getOne(Wrappers.<Device>lambdaQuery()
                .eq(Device::getDeviceSn, entity.getDeviceSn())
                .ne(Device::getId, entity.getId())).getData();
        if(device != null) {
            return new BooleanResult(false).fail("设备编号已经存在["+entity.getDeviceSn()+"]");
        }
        return super.updateById(entity);
    }

    @Override
    public void update(String deviceSn, DeviceStatus status) {
        update(Wrappers.<Device>lambdaUpdate()
                .set(Device::getStatus, status)
                .set(Device::getSwitchTime, new Date())
                .eq(Device::getDeviceSn, deviceSn));
    }

    @Override
    public DetailResult<Device> getByDeviceSn(String deviceSn) {
        return getOne(Wrappers.<Device>lambdaQuery().eq(Device::getDeviceSn, deviceSn));
    }

    @Override
    public CurrentDeviceDto countCurrentDevice() {
        return getBaseMapper().countCurrentDevice();
    }
}
